package com.ruoyi.ktv.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ktv.domain.KtvProduct;
import com.ruoyi.ktv.service.IKtvProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ktv.domain.KtvSubOrder;
import com.ruoyi.ktv.service.IKtvSubOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 子订单管理Controller
 * 
 * @author xubiao
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/ktv/subOrder")
public class KtvSubOrderController extends BaseController
{
    @Autowired
    private IKtvSubOrderService ktvSubOrderService;

    @Autowired
    private IKtvProductService iKtvProductService;

    /**
     * 查询子订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(KtvSubOrder ktvSubOrder)
    {
        startPage();
        List<KtvSubOrder> list = ktvSubOrderService.selectKtvSubOrderList(ktvSubOrder);
        return getDataTable(list);
    }

    /**
     * 导出子订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:export')")
    @Log(title = "子订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KtvSubOrder ktvSubOrder)
    {
        List<KtvSubOrder> list = ktvSubOrderService.selectKtvSubOrderList(ktvSubOrder);
        ExcelUtil<KtvSubOrder> util = new ExcelUtil<KtvSubOrder>(KtvSubOrder.class);
        util.exportExcel(response, list, "子订单管理数据");
    }

    /**
     * 获取子订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:query')")
    @GetMapping(value = "/{subOrderId}")
    public AjaxResult getInfo(@PathVariable("subOrderId") Long subOrderId)
    {
        return success(ktvSubOrderService.selectKtvSubOrderBySubOrderId(subOrderId));
    }

    /**
     * 新增子订单管理
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:add')")
    @Log(title = "子订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KtvSubOrder ktvSubOrder)
    {
        if(null != ktvSubOrder.getProductId() && (null == ktvSubOrder.getProductName() || ktvSubOrder.getProductName().isEmpty())){
            Long productId = ktvSubOrder.getProductId();
            KtvProduct ktvProduct = iKtvProductService.selectKtvProductByProductId(productId);
            ktvSubOrder.setProductName(ktvProduct.getProductName());
            ktvSubOrder.setAmountReceivable(ktvProduct.getProductPrice()*ktvSubOrder.getQuantity());
    }
        return toAjax(ktvSubOrderService.insertKtvSubOrder(ktvSubOrder));
    }

    /**
     * 修改子订单管理
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:edit')")
    @Log(title = "子订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KtvSubOrder ktvSubOrder)
    {
        return toAjax(ktvSubOrderService.updateKtvSubOrder(ktvSubOrder));
    }

    /**
     * 删除子订单管理
     */
    @PreAuthorize("@ss.hasPermi('ktv:subOrder:remove')")
    @Log(title = "子订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subOrderIds}")
    public AjaxResult remove(@PathVariable Long[] subOrderIds)
    {
        return toAjax(ktvSubOrderService.deleteKtvSubOrderBySubOrderIds(subOrderIds));
    }
}
